package com.atguigu.gulimall.order.listener;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;


@RabbitListener(queues = {"order.release.order.queue"})
@Service
public class OrderCloseListener {

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void listener(OrderEntity order, Message message, Channel channel) throws IOException {
        System.out.println("接收到过期订单，准备关闭订单，进行解锁库存操作");
        try {
            orderService.orderClose(order);
            //basicAck(long deliveryTag, boolean multiple)
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            //拒签，归队
            //basicReject(long deliveryTag, boolean requeue)
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }
}
